package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.DataDictionaryItem;
import cn.wolfcode.crm.mapper.DataDictionaryItemMapper;
import cn.wolfcode.crm.mapper.DataDictionaryMapper;
import cn.wolfcode.crm.query.DictionaryQueryObject;
import cn.wolfcode.crm.service.IDataDictionaryItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataDictionaryItemServiceImpl implements IDataDictionaryItemService {
    @Autowired
    private DataDictionaryItemMapper dataDictionaryItemMapper;
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;



    public List<DataDictionaryItem> selectAll(DictionaryQueryObject qo) {
        if(qo.getSn()!=null){
            Long parentId = dataDictionaryMapper.selectBySn(qo);
            return dataDictionaryItemMapper.selectAll(parentId);
        }
        return null;
    }

    public Boolean selectByName(Long id, String name) {
        DataDictionaryItem dataDictionaryItem = dataDictionaryItemMapper.selectByName(name);
        return dataDictionaryItem==null||dataDictionaryItem.getId()==id;
    }

    public void saveOrUpdate(DataDictionaryItem dataDictionaryItem) {
        /*维护关系*/
        if (dataDictionaryItem.getId() == null) {
            dataDictionaryItemMapper.insert(dataDictionaryItem);
        }else{
            dataDictionaryItemMapper.updateByPrimaryKey(dataDictionaryItem);
        }
    }

    public void delete(Long id) {
        if(id!=null){
            dataDictionaryItemMapper.deleteByPrimaryKey(id);
        }
    }
}
